﻿using System.Data;
using Microsoft.AspNetCore.Builder;
using SqlSugar;

namespace DFGJ.WMS_PDA.WebAPI.Extensions
{   
    /// <summary>
    /// SqlSugar扩展
    /// </summary>
    public static class SqlSugarExtend
    {

        public static void AddSqlSugarSetUp(this WebApplicationBuilder builder)
        {
            string connectionString = builder.Configuration.GetSection("ConnectionStrings").GetSection("DFGK_WMS").Value;
            if (string.IsNullOrEmpty(connectionString))
            {
                throw new Exception("配置文件不能为空");
            }
            builder.Services.AddTransient<ISqlSugarClient>(s =>
            {
                ConnectionConfig connection = new ConnectionConfig()
                {
                    ConnectionString = connectionString,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true

                };
                SqlSugarClient client = new SqlSugarClient(connection);
                client.Aop.OnLogExecuting = (s, p) =>
                {
                    Console.WriteLine("*******************************************************************************************");
                    Console.WriteLine($"OnLogExecuting:输出Sql语句:{s} || 参数为：{string.Join(",", p.Select(p => p.Value))}");
                };  
                return client;
            });
        }

    }
}
